#ifndef _REGION_H_
#define _REGION_H_


#include "Common.h"
#include "String.h"
#include "SharedPtr.h"
#include "Resource.h"
#include "Vector3.h"
#include "Effects.h"
#include "Towns.h"
#include "Threading/AtomicBoolean.h"

class CharPtr;
class ItemPtr;
class TownPtr;
class EffectPtr;
class SpawnPtr;
class ChannelPtr;

//物品公共属性
typedef struct region_common_property
{

	Vector3		position;
	
	ResourceProxy		uplink; //父级对象连接
	void initialize(void)
	{
		position = Vector3::ZERO;
		uplink.setNull();
	}

	region_common_property()
	{
		initialize();
	}

}region_common_property_t;

class Region :public Resource
{
public:
	enum RegionStatus
	{
		RegionStatusNone			= 0,	//无效状态
		RegionStatusNormal		= 1,	//一般状态
		RegionStatusUse			= 2,	//使用，装备，作用状态
		RegionStatusFreeze		= 3,	//物品冻结状态
		RegionStatusDeath			= 4	//损坏，死亡状态
	};

	Region(ResourceManager* creator, const String& name, ResourceHandle handle,
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
	~Region(void);

protected:
       	void prepareImpl(void);
        /** Destroys data cached by prepareImpl.
         */
        void unprepareImpl(void);
        /// @copydoc Resource::loadImpl
        bool loadImpl(void);
        /// @copydoc Resource::postLoadImpl
        void postLoadImpl(void);

	 void preUnloadImpl(void);
        /// @copydoc Resource::unloadImpl
        void unloadImpl(void);
        /// @copydoc Resource::calculateSize
        size_t calculateSize(void) const;

private:
	std::list<ResourceProxy>	m_chars;	// 区域角色数量
	std::list<ResourceProxy>	m_items;	// 如果为容器物品，包含的物品序列号
	std::list<ResourceProxy>	m_towns;	// 区域中的城市
	std::list<ResourceProxy>	m_effects;	// 区域中作用的效果
	std::list<ResourceProxy>	m_spawns;	// 区域中的再生区域
	std::list<ResourceProxy>	m_channels;	// 区域中的战场

	uint32		m_serial;
	uint32		m_group_id;

	//
	uint16			m_region_x;
	uint16			m_region_y;

	bool            m_idle;
	uint32          m_timelast;

	void    initialize(void);
public:
	bool		mProcess;

	const uint32  & getSerial() 	{	return m_serial;	}
	const uint32  & getGroupId()	{	return m_group_id;	}

	const uint16 & getRegionX(void)	{	return m_region_x;	}
	const uint16 & getRegionY(void)	{	return m_region_y;	}


	//设置处理标识为忙，避免重复队列处理
        void    setBusy(void)           {       m_idle = false; }
        void    setIdle(void)           {       m_idle = true;  }
	bool    isBusy(void)            {       return (m_idle == false);       }

	//检查是否进行自动更新
	bool    checkUpdate(const uint32 & timenow)     {  return (m_idle && timenow > m_timelast) ? true:false;}
        void    updateTimeLast(const uint32 & timelast) {       m_timelast = timelast;  }


	//region auto check
        bool    run(const uint32 & p_time);
	//check Sibling process
	bool	isSiblingProcess(void);


	//添加释放区域物品包含的角色
	bool	addChar(const uint32 & serial);
	bool  	addChar(ResourceProxy & ch);
	bool	removeChar(const uint32 & serial);
	bool	removeChar(ResourceProxy  & ch);
	bool	deleteChar(const uint32 & serial);
	bool	deleteChar(ResourceProxy & ch);
	bool	destroyChar(const uint32 & serial);
	bool	destroyChar(ResourceProxy & ch);
	void	removeAllChars(void);
	void	deleteAllChars(void);
	void	destroyAllChars(void);
	
	//添加释放区域包含的物品
	bool	addItem(const uint32 & serial);
	bool  	addItem(ResourceProxy & item);
	bool	removeItem(const uint32 & serial);
	bool	removeItem(ResourceProxy  & item);
	bool	deleteItem(const uint32 & serial);
	bool	deleteItem(ResourceProxy & item);
	bool	destroyItem(const uint32 & serial);
	bool	destroyItem(ResourceProxy & item);
	void	removeAllItems(void);
	void	deleteAllItems(void);
	void	destroyAllItems(void);
	
	//添加效果到区域
	bool	addEffect(const uint32 & serial);
	bool	addEffect(ResourceProxy & effect);
	bool	removeEffect(const uint32 & serial);
	bool	removeEffect(ResourceProxy & effect);
	bool    deleteEffect(const uint32 & serial);
	bool	deleteEffect(ResourceProxy & effect);
	bool    destroyEffect(const uint32 & serial);
	bool	destroyEffect(ResourceProxy & effect);
	void	removeAllEffects(void);
	void	deleteAllEffects(void);
	void	destroyAllEffects(void);

	//添加释放区域的城市
	bool	addTown(const uint32 & serial);
	bool  	addTown(ResourceProxy & town);
	bool	removeTown(const uint32 & serial);
	bool	removeTown(ResourceProxy  & town);
	bool	deleteTown(const uint32 & serial);
	bool	deleteTown(ResourceProxy & town);
	bool	destroyTown(const uint32 & serial);
	bool	destroyTown(ResourceProxy & town);
	void	removeAllTowns(void);
	void	deleteAllTowns(void);
	void	destroyAllTowns(void);
	
	//添加释放区域再生控制对象
	bool	addSpawn(const uint32 & serial);
	bool  	addSpawn(ResourceProxy & spawn);
	bool	removeSpawn(const uint32 & serial);
	bool	removeSpawn(ResourceProxy  & spawn);
	bool	deleteSpawn(const uint32 & serial);
	bool	deleteSpawn(ResourceProxy & spawn);
	bool	destroySpawn(const uint32 & serial);
	bool	destroySpawn(ResourceProxy & spawn);
	void	removeAllSpawns(void);
	void	deleteAllSpawns(void);
	void	destroyAllSpawns(void);
	
	//添加释放区域再生控制对象
	bool	addChannel(const uint32 & serial);
	bool  	addChannel(ResourceProxy & channel);
	bool	removeChannel(const uint32 & serial);
	bool	removeChannel(ResourceProxy  & channel);
	bool	deleteChannel(const uint32 & serial);
	bool	deleteChannel(ResourceProxy & channel);
	bool	destroyChannel(const uint32 & serial);
	bool	destroyChannel(ResourceProxy & channel);
	void	removeAllChannels(void);
	void	deleteAllChannels(void);
	void	destroyAllChannels(void);

	//得到包含物品的列表复制
	std::list<ItemPtr> * getItemsList(std::list<ItemPtr> * list);

	//得到城市的列表
	std::list<TownPtr> * getTownsList(std::list<TownPtr> * list);

	//得到角色列表
	std::list<CharPtr> * getCharsList(std::list<CharPtr> * list);

	//得到包括特效的列表复制
	std::list<EffectPtr> * getEffectsList(std::list<EffectPtr> * list);

	//得到包括再生对象列表复制
	std::list<SpawnPtr> * getSpawnsList(std::list<SpawnPtr> * list);
	
	//得到战场对象
	std::list<ChannelPtr> * getChannelsList(std::list<ChannelPtr> * list);

	//save item to database	
	bool	SaveDB(void);
	void	Delete(void);
	void	Destroy(void);

	size_t	loadCharsFormDB(void);
	size_t  loadItemsFromDB(void);
        size_t  loadEffectsFromDB(void);


	//区域关联的地图
	//WorldMapPtr	worldMap;

	//物品公共属性
	region_common_property_t	common;
};

    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to ItemPtr 
    @note Has to be a subclass since we need operator=.
    We could templatise this instead of repeating per Resource subclass, 
    except to do so requires a form VC6 does not support i.e.
    ResourceSubclassPtr<T> : public SharedPtr<T>
    */
    class SERVER_DECL  RegionPtr : public SharedPtr<Region>
    {
    public:
        RegionPtr() : SharedPtr<Region>() {}
        explicit RegionPtr(Region* rep) : SharedPtr<Region>(rep) {}
        RegionPtr(const RegionPtr& r) : SharedPtr<Region>(r) {}
        RegionPtr(const ResourcePtr& r);
        /// Operator used to convert a ResourcePtr to a MeshPtr
        RegionPtr& operator=(const ResourcePtr& r);
    protected:
        /// Override destroy since we need to delete Mesh after fully defined
        void destroy(void);
    };
	

#endif
